Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * tjp from https://gist.github.com/FredEckert/3425429
- * 14mar202 runs on rpi well
- * i want arcs and widgets, so lookig at 'nuklear; to get a lot done for me
- *
- * ver 1 had pixel and line dwg ok
- * but had to limit y range to vinfo.yres-7 huh?
- * this is ver 2 add point line rect filledrect circle filledcrcle DONE
- * TODO: add arc, line width, mouse, btn,led, textbox....
- *
- To test that the Linux framebuffer is set up correctly, and that the device permissions
- are correct, use the program below which opens the frame buffer and draws a gradient-
- filled red square:
- retrieved from:
- Testing the Linux Framebuffer for Qtopia Core (qt4-x11-4.2.2)
- http://cep.xor.aps.anl.gov/software/qt4-x11-4.2.2/qtopiacore-testingframebuffer.html
- * tjp this builds ok and works on ^altF1
- * isee a square maybe 200x200 with reddish gradient ll corner to tr corner
- * why so small? becuz the code asid be small
- * now full screen except for the command line at bottom (1366*768)
- * */
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <fcntl.h>
- #include <linux/fb.h>
- #include <sys/mman.h>
- #include <sys/ioctl.h>
- // 'global' variables to store screen info
- struct fb_var_screeninfo vinfo;
- struct fb_fix_screeninfo finfo;
- char *fbp = 0;
- // helper function to 'plot' a pixel in given color
- // tjp clean up the cr cg cu ca, make it one thing and pass ptr to it
- // tjp this is now hard coded for 32bpp, and ca doesnt wokr yet ( transparency )
- void put_pixel(int x, int y, int cr, int cg, int cu, int ca)
- {
- long int location = 0;
- int myx, myy, yfudge;
- yfudge = 7;
- myx = x+vinfo.xoffset;
- myy = y+vinfo.yoffset;
- // CLIP all dwg to FRAME BUFFER BOUNDS INCLUDING DANGER SONE AT BOTTOM 'FUDGE'
- if ((myx < vinfo.xres) && ( myx > -1) && (myy < vinfo.yres - yfudge) && (myy > -1 )) {
- location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
- (y+vinfo.yoffset) * finfo.line_length;
- *(fbp + location) = cr;
- *(fbp + location + 1) = cg;
- *(fbp + location + 2) = cu;
- *(fbp + location + 3) = ca;
- }
- }
- // (uses Bresenham's line algorithm)
- void draw_line(int x0, int y0, int x1, int y1, int cr, int cg, int cu,int ca )
- {
- int dx;
- int dy;
- int sx;
- int sy;
- int err;
- int e2;
- int done;
- dx = x1 - x0;
- dx = (dx >= 0) ? dx : -dx; // abs()
- dy = y1 - y0;
- dy = (dy >= 0) ? dy : -dy; // abs()
- if (x0 < x1)
- sx = 1;
- else
- sx = -1;
- if (y0 < y1)
- sy = 1;
- else
- sy = -1;
- err = dx - dy;
- done = 0;
- while (!done) {
- put_pixel(x0, y0, cr, cg, cu, ca );
- if ((x0 == x1) && (y0 == y1))
- done = 1;
- else {
- e2 = 2 * err;
- if (e2 > -dy) {
- err = err - dy;
- x0 = x0 + sx;
- }
- if (e2 < dx) {
- err = err + dx;
- y0 = y0 + sy;
- }
- }
- }
- }
- // helper function to draw a rectangle outline in given color
- void draw_rect(int x0, int y0, int w, int h, int cr, int cg, int cu, int ca) {
- draw_line(x0, y0, x0 + w, y0, cr, cg, cu, ca); // top
- draw_line(x0, y0, x0, y0 + h, cr, cg, cu, ca); // left
- draw_line(x0, y0 + h, x0 + w, y0 + h, cr, cg, cu, ca); // bottom
- draw_line(x0 + w, y0, x0 + w, y0 + h, cr, cg, cu, ca); // right
- }
- // helper function to draw a rectangle outline in given color
- void fill_rect(int x0, int y0, int w, int h, int cr, int cg, int cu, int ca) {
- int y;
- for (y = 0; y < h; y++) {
- draw_line(x0, y0 + y, x0 + w, y0 + y, cr, cg, cu, ca);
- }
- }
- // helper function to draw a circle outline in given color
- // (uses Bresenham's circle algorithm)
- void draw_circle(int x0, int y0, int r, int cr, int cg, int cu, int ca)
- {
- int x = r;
- int y = 0;
- int radiusError = 1 - x;
- while(x >= y)
- {
- put_pixel(-y + x0, -x + y0, ca, cg, cu, ca);// top left
- put_pixel( y + x0, -x + y0, ca, cg, cu, ca);// top right
- put_pixel(-x + x0, -y + y0, ca, cg, cu, ca);// upper middle left
- put_pixel( x + x0, -y + y0, ca, cg, cu, ca);// upper middle right
- put_pixel(-x + x0, y + y0, ca, cg, cu, ca);// lower middle left
- put_pixel( x + x0, y + y0, ca, cg, cu, ca);// lower middle right
- put_pixel(-y + x0, x + y0, ca, cg, cu, ca);// bottom left
- put_pixel( y + x0, x + y0, ca, cg, cu, ca);// bottom right
- y++;
- if (radiusError < 0)
- {
- radiusError += 2 * y + 1;
- } else {
- x--;
- radiusError+= 2 * (y - x + 1);
- }
- }
- }
- // helper function to draw a filled circle in given color
- // (uses Bresenham's circle algorithm)
- void fill_circle(int x0, int y0, int r, int cr, int cg, int cu, int ca) {
- int x = r;
- int y = 0;
- int radiusError = 1 - x;
- while(x >= y)
- {
- draw_line(-y + x0, -x + y0, y + x0, -x + y0, ca, cg, cu, ca);// top
- draw_line(-x + x0, -y + y0, x + x0, -y + y0, ca, cg, cu, ca);// upper middle
- draw_line(-x + x0, y + y0, x + x0, y + y0, ca, cg, cu, ca);// lower middle
- draw_line(-y + x0, x + y0, y + x0, x + y0, ca, cg, cu, ca);// bottom
- y++;
- if (radiusError < 0)
- {
- radiusError += 2 * y + 1;
- } else {
- x--;
- radiusError+= 2 * (y - x + 1);
- }
- }
- }
- int main()
- {
- int fbfd = 0;
- int x = 0, y = 0, r = 0, cr = 0, cg = 0,cu = 0, w = 0, h = 9;
- long int screensize = 0;
- // Open the file for reading and writing
- fbfd = open("/dev/fb0", O_RDWR);
- if (fbfd == -1) {
- perror("Error: cannot open framebuffer device");
- exit(1);
- }
- // printf("The framebuffer device was opened successfully.\n");
- // Get fixed screen information
- if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1) {
- perror("Error reading fixed information");
- exit(2);
- }
- // Get variable screen information
- if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
- perror("Error reading variable information");
- exit(3);
- }
- // printf("fb %dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
- // Figure out the size of the screen in bytes
- screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
- // Map the device to memory
- fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
- if ((long int)fbp == -1) {
- perror("Error: failed to map framebuffer device to memory");
- exit(4);
- }
- // printf("The framebuffer device was mapped to memory successfully.\n");
- // tjp bg color fill ( bg becuz its 1st thing drawn ( under all followers )
- fill_rect(0,0,vinfo.xres - 1,vinfo.yres - 7, 100,15,200,0);
- // horz line mid screen i ask for vinfo.yres but putpixel will limit to vinfo.yres-7
- draw_line(0,(vinfo.yres)/2,vinfo.xres,(vinfo.yres)/2,0xff,0xff,0xff,0xff);
- // vert line midscreen
- draw_line(vinfo.xres/2,0,vinfo.xres/2,vinfo.yres,0xff,0xff,0xff,0xff);
- // big X thru whole screen
- draw_line(0,0,vinfo.xres,vinfo.yres,0xff,0xff,0xff,0xff);
- draw_line(0,vinfo.yres,vinfo.xres,0,0xff,0xff,0xff,0xff);
- x = 100;
- y = 200;
- w = 200;
- h = 150;
- for ( x = 100; x < vinfo.xres + 100; x = x + 25 ) {
- draw_rect(x,y,w,h, 0x00,0xff,0x00,0x08);
- y = y + 15;
- }
- x = vinfo.xres - 200 - 100;
- y = 100;
- w = 200;
- h = 50;
- for ( x = vinfo.xres - 200 - 100; x > 0; x = x - 25 ) {
- fill_rect(x,y,w,h, 0x00,0xff,0x00,0xff);
- y = y +17;
- }
- x = 200;
- y = vinfo.yres - 200;
- for ( r= 10; r<400; r+=15) {
- draw_circle(x,y,r,0x00,0x80,0x80,0x80);
- x = x + 10;
- y = y - 5;
- }
- cr = 100;
- cg = 255;
- cu = 0x00;
- x = vinfo.xres - 200;
- y = vinfo.yres - 200;
- for ( r = 10; r<250; r+=10){
- fill_circle(x,y,r,cu,cu,cu,0xff);
- cr = cr + 5;
- cg = cg - 8;
- cu = cu + 10;
- x = x -20;
- y = y -20;
- }
- // tjp stupid delay
- for( x=0; x<65536; x++)
- for( y=0; y<15536; y++);
- munmap(fbp, screensize);
- close(fbfd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement